package com.longdaji.decoration.ui.webview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.utils.LogUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class WebViewActivity extends BaseActivity implements WebViewContract.View {

    private static final String TAG = WebViewActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.web_view)
    WebView mWebView;
    @Inject
    WebViewPresenter mPresenter;


    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, WebViewActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "WebViewActivity onCreate");
        setContentView(R.layout.activity_webview);
        setUnBinder(ButterKnife.bind(this));
        initView();
    }


    @SuppressLint("NewApi")
    private void initView() {
        mPresenter.setView(this);
        String title = "";
        int authType = 1;
        if (getIntent() != null) {
            title = getIntent().getStringExtra("title");
            authType = getIntent().getIntExtra("auth_type", 1);
        }
        mTvTitle.setText(title);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webSettings.setJavaScriptEnabled(true);
        // 优先使用缓存优化效率
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                if (url.contains("http://120.55.48.242")) {
                    setResult(RESULT_OK);
                    finish();
                }
                LogUtil.d(TAG, "onPageStarted url" + url);
                super.onPageStarted(view, url, favicon);
            }

        });
        getData(authType);
    }

    private void getData(int authType) {
        mPresenter.getUrl(Account.getInstance().getUserid(), authType);
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    public void loadWebViewUrl(String url) {
        mWebView.loadUrl(url);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 检查是否为返回事件，如果有网页历史记录
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        // 如果不是返回键或没有网页浏览历史，保持默认
        // 系统行为（可能会退出该活动）
        return super.onKeyDown(keyCode, event);

    }
}
